package com.doupi.partner.service.impl;

import com.doupi.partner.dal.BusinessRelationMapper;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessRelationServiceImpl implements BusinessRelationService {
	private final BusinessRelationMapper businessRelationMapper;

	@Autowired
	public BusinessRelationServiceImpl(BusinessRelationMapper businessRelationMapper) {
		this.businessRelationMapper = businessRelationMapper;
	}

	public int deleteByPrimaryKey(Long id) {
		return businessRelationMapper.deleteByPrimaryKey(id);
	}

	public int insert(BusinessRelation record) {
		return businessRelationMapper.insert(record);
	}

	public BusinessRelation selectByPrimaryKey(Long id) {
		return businessRelationMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(BusinessRelation record) {
		return businessRelationMapper.updateByPrimaryKeySelective(record);
	}

	public int countBusinessRelation(BusinessRelation businessRelation) {
		return businessRelationMapper.countBusinessRelation(businessRelation);
	}

	public List<BusinessRelation> queryBusinessRelations(BusinessRelation businessRelation, RowBounds rowBounds) {
		return businessRelationMapper.queryBusinessRelations(businessRelation, rowBounds);
	}

	public List<BusinessRelation> selectBusinessRelationForUpdate(BusinessRelation businessRelation) {
		return businessRelationMapper.selectBusinessRelationForUpdate(businessRelation);
	}

	public BusinessRelation selectBusinessRelationForUpdateByPrimaryKey(Long id) {
		return businessRelationMapper.selectBusinessRelationForUpdateByPrimaryKey(id);
	}

	public Pagination<BusinessRelation, BusinessRelation> queryBusinessRelations(Pagination<BusinessRelation, BusinessRelation> pagination) {
		int count = countBusinessRelation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryBusinessRelations(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public int saveBusinessRelation(BusinessRelation businessRelation) {
		int flag;
		if (businessRelation.getId() == null || businessRelation.getId() <= 0) {
			flag = insert(businessRelation);
		} else {
			flag = updateByPrimaryKeySelective(businessRelation);
		}
		return flag;
	}

	@Override
	public int countBusinessRelationWithAnnotation(BusinessRelation businessRelation) {
		return businessRelationMapper.countBusinessRelationWithAnnotation(businessRelation);
	}

	@Override
	public List<BusinessRelation> queryBusinessRelationsWithAnnotation(BusinessRelation businessRelation, RowBounds rowBounds) {
		return businessRelationMapper.queryBusinessRelationsWithAnnotation(businessRelation, rowBounds);
	}

	@Override
	public Pagination<BusinessRelation, BusinessRelation> queryBusinessRelationsWithAnnotation(Pagination<BusinessRelation, BusinessRelation> pagination) {
		int count = countBusinessRelationWithAnnotation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryBusinessRelationsWithAnnotation(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public BusinessRelation selectBusinessRelationByBusinessId(String businessId) {
		BusinessRelation businessRelation = new BusinessRelation();
		businessRelation.setBusinessId(businessId);
		List<BusinessRelation> list = queryBusinessRelations(businessRelation, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

}
